package widget;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JComponent;
import callback.Callback;

public class MyButtonWidget extends JComponent{

	/**
	 * 
	 */
	private static final long serialVersionUID = -3556544246445189318L;
	protected int mPosX;
	protected int mPosY;
	private Image mBackgroundEnable;
	private Image mBackgroundDisable;
	protected String mText;
	private Callback mCallback;
	private Rectangle mBounder;
	private boolean mIsEnable;
	private int mTextPosDeltaX = 0;
	private int mTextPosDeltaY = 0;
	

	public MyButtonWidget(int posX, int posY, String backgroundEnable, String backgoundDisable, String text, Callback callback) {
		this.mPosX = posX;
		this.mPosY = posY;
		try {
			this.mBackgroundEnable = ImageIO.read(new File(backgroundEnable));
			this.mBackgroundDisable = ImageIO.read(new File(backgoundDisable));
		} catch (IOException e) {
			e.printStackTrace();
		}
		this.mText = text;
		this.mCallback = callback;
		this.mBounder = new Rectangle(posX, posY, mBackgroundEnable.getWidth(null), mBackgroundEnable.getHeight(null));
		this.mIsEnable = true;
	}

	public MyButtonWidget(int posX, int posY, String backgroundEnable, String backgoundDisable, 
			String text, int textPosDeltaX, int textPosDeltaY, Callback callback) {
		this(posX, posY, backgroundEnable,backgoundDisable, text, callback);
		mTextPosDeltaX = textPosDeltaX;
		mTextPosDeltaY = textPosDeltaY;
	}
 
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		
		if(mBackgroundEnable != null && mIsEnable) {
			g.drawImage(mBackgroundEnable, mPosX, mPosY, null);
		}
		else {
			g.drawImage(mBackgroundDisable, mPosX, mPosY, null);
		}
		
		g.setColor(Color.magenta);
		g.setFont(new Font("Buxton Sketch", Font.BOLD, 30));
		g.drawString(mText, mPosX + mTextPosDeltaX, mPosY + mTextPosDeltaY);
	}
	
	public void onClick(MouseEvent e) {
		if(mBounder.contains(e.getX(), e.getY()) && mIsEnable) {
			mCallback.perform();
		}
	}
	
	public void enable() {
		this.mIsEnable = true;
	}
	
	public void disable() {
		this.mIsEnable = false;
	}
}
